package com.dp.study.WebSocket.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

/**
 * Created by IntelliJ IDEA
 * User: DengPeng
 * Date: 2019/5/15 ,
 * Time: 17:54 ,
 * Email:dp9212@163.com .
 */
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    /**
     * 注册端点，发布或订阅消息需要连接此端点
     * setAllowedOrigins 非必须，*允许其他域连接
     * withSockJS 启用socketjs支持
     * @param registry
     */
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/gs-guide-websocket").setAllowedOrigins("*").withSockJS();
    }

    /**
     * 配置消息代理
     * enableSimpleBroker  服务端推送给客户端的路径前缀
     * setApplicationDestinationPrefixes  客户端发送数据给服务端的前缀
     * @param config
     */
    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic","/chat");
        config.setApplicationDestinationPrefixes("/app");
    }

}
